import { message } from "antd";

export function createUniqueId() {
    const buf = new Uint32Array(1);
    crypto.getRandomValues(buf);
    return buf[0];
}

export async function runProg(msg: string, fc: () => Promise<void>) {
    const key = createUniqueId();
    message.loading({
        key,
        content: msg,
        duration: 0,
    });
    try {
        await fc();
        message.success({
            key,
            duration: 1,
            content: `${msg}成功`,
            onClick: () => { message.destroy(key); }
        });
    } catch (e) {
        message.error({
            content: e as string,
            duration: 0,
            key,
            onClick: () => {
                message.destroy(key);
            }
        }, 0);
    }
}
